#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > cc_geoopt2.info <<'%EOF%'
   cc_geoopt2
   ----------
   Molecule:         CO in C1
   Wave Function:    CCSD
   Test Purpose:     Geometry optimization using the analytic CCSD
                     gradient and the *OPTIMIZE module
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > cc_geoopt2.mol <<'%EOF%'
INTGRL
first order property calculation on CO                                          
dunning-basis                                                                   
    2  0 0  X  Y  Z   0.10D-14
        6.    1    3    1    1    1
C        0.0000000000000     0.0000000000000     1.2282431300000
H  18    4
     6665.0000000000        0.0006920000        0.0000000000        0.0000000000
                            0.0000000000
     1000.0000000000        0.0053290000        0.0000000000        0.0000000000
                            0.0000000000
      228.0000000000        0.0270770000        0.0000000000        0.0000000000
                            0.0000000000
       64.7100000000        0.1017180000        0.0000000000        0.0000000000
                            0.0000000000
       21.0600000000        0.2747400000        0.0000000000        0.0000000000
                            0.0000000000
        7.4950000000        0.4485640000        0.0000000000        0.0000000000
                            0.0000000000
        2.7970000000        0.2850740000        0.0000000000        0.0000000000
                            0.0000000000
        0.5215000000        0.0152040000        0.0000000000        0.0000000000
                            0.0000000000
     6665.0000000000        0.0000000000       -0.0001460000        0.0000000000
                            0.0000000000
     1000.0000000000        0.0000000000       -0.0011540000        0.0000000000
                            0.0000000000
      228.0000000000        0.0000000000       -0.0057250000        0.0000000000
                            0.0000000000
       64.7100000000        0.0000000000       -0.0233120000        0.0000000000
                            0.0000000000
       21.0600000000        0.0000000000       -0.0639550000        0.0000000000
                            0.0000000000
        7.4950000000        0.0000000000       -0.1499810000        0.0000000000
                            0.0000000000
        2.7970000000        0.0000000000       -0.1272620000        0.0000000000
                            0.0000000000
        0.5215000000        0.0000000000        0.5445290000        0.0000000000
                            0.0000000000
        0.1596000000        0.0000000000        0.0000000000        1.0000000000
                            0.0000000000
        0.0469000000        0.0000000000        0.0000000000        0.0000000000
                            1.0000000000
H   5    3
        9.4390000000        0.0381090000        0.0000000000        0.0000000000
        2.0020000000        0.2094800000        0.0000000000        0.0000000000
        0.5456000000        0.5085570000        0.0000000000        0.0000000000
        0.1517000000        0.0000000000        1.0000000000        0.0000000000
        0.0404100000        0.0000000000        0.0000000000        1.0000000000
H   2    2
        0.5500000000        1.0000000000        0.0000000000
        0.1510000000        0.0000000000        1.0000000000
        8.    1    3    1    1    1
O        0.0000000000000     0.0000000000000    -0.9139731000000
H  18    4
    11720.0000000000        0.0007100000        0.0000000000        0.0000000000
                            0.0000000000
     1759.0000000000        0.0054700000        0.0000000000        0.0000000000
                            0.0000000000
      400.8000000000        0.0278370000        0.0000000000        0.0000000000
                            0.0000000000
      113.7000000000        0.1048000000        0.0000000000        0.0000000000
                            0.0000000000
       37.0300000000        0.2830620000        0.0000000000        0.0000000000
                            0.0000000000
       13.2700000000        0.4487190000        0.0000000000        0.0000000000
                            0.0000000000
        5.0250000000        0.2709520000        0.0000000000        0.0000000000
                            0.0000000000
        1.0130000000        0.0154580000        0.0000000000        0.0000000000
                            0.0000000000
    11720.0000000000        0.0000000000       -0.0001600000        0.0000000000
                            0.0000000000
     1759.0000000000        0.0000000000       -0.0012630000        0.0000000000
                            0.0000000000
      400.8000000000        0.0000000000       -0.0062670000        0.0000000000
                            0.0000000000
      113.7000000000        0.0000000000       -0.0257160000        0.0000000000
                            0.0000000000
       37.0300000000        0.0000000000       -0.0709240000        0.0000000000
                            0.0000000000
       13.2700000000        0.0000000000       -0.1654110000        0.0000000000
                            0.0000000000
        5.0250000000        0.0000000000       -0.1169550000        0.0000000000
                            0.0000000000
        1.0130000000        0.0000000000        0.5573680000        0.0000000000
                            0.0000000000
        0.3023000000        0.0000000000        0.0000000000        1.0000000000
                            0.0000000000
        0.0789600000        0.0000000000        0.0000000000        0.0000000000
                            1.0000000000
H   5    3
       17.7000000000        0.0430180000        0.0000000000        0.0000000000
        3.8540000000        0.2289130000        0.0000000000        0.0000000000
        1.0460000000        0.5087280000        0.0000000000        0.0000000000
        0.2753000000        0.0000000000        1.0000000000        0.0000000000
        0.0685600000        0.0000000000        0.0000000000        1.0000000000
H   2    2
        1.1850000000        1.0000000000        0.0000000000
        0.3320000000        0.0000000000        1.0000000000
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > cc_geoopt2.dal <<'%EOF%'
**DALTON INPUT
.DIRECT
.OPTIMIZE
**INTEGRAL
.DIPLEN
.DEROVL
.DERHAM
**WAVE FUNCTION
.CC
*SCF INP
.THRESH
 1.0D-10
.DOUBLY OCCUPIED
 7
*CC INP
.CCSD
.PRINT
 6
.RESTART
.THRENR
1.0D-10
.THRLEQ
1.0D-09
.NSYM
1
.FROIMP
2
0
**END OF DALTON INPUT
%EOF%
#######################################################################

 
#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >cc_geoopt2.check
cat >>cc_geoopt2.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

if $GREP -q "not implemented for parallel calculations" $log; then
   echo "TEST ENDED AS EXPECTED"
   exit 0
fi

# SCF, MP2 and CCSD energies of 0 iteration:
CRIT1=`$GREP "Final * HF energy: * -112\.75415775" $log | wc -l`
# CRIT2=`$GREP "Total MP2   energy: * -113\.05440118" $log | wc -l`
CRIT3=`$GREP "Total CCSD  energy: * -113\.06120700" $log | wc -l`
CRIT4=`$GREP " C * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( \-|\-0)\.016355" $log | wc -l`
CRIT5=`$GREP " O * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( |0)\.016355" $log | wc -l`
CRIT6=`$GREP "Energy at this geometry is * : * -113\.061207" $log | wc -l`
CRIT7=`$GREP "Norm of gradient * : * ( |0)\.023130" $log | wc -l`
CRIT8=`$GREP "Norm of step * : * ( |0)\.01308" $log | wc -l`
TEST[1]=`expr $CRIT1 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8` 
#        expr 1 + 0 + 3 + 1 + 1 + 1 + 1 + 1
CTRL[1]=9
ERROR[1]="INITIAL ITERATION NOT CORRECT"

# SCF, MP2 and CCSD energies of 1 iteration:
CRIT1=`$GREP "Final * HF energy: *-112\.75326688" $log | wc -l`
CRIT3=`$GREP "Total CCSD  energy: *-113\.0613128[4-6]" $log | wc -l`
CRIT4=`$GREP " C * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( |0)\.000047" $log | wc -l`
CRIT5=`$GREP " O * ( |0|\-0|\-)\.0000000000 * ( |0|\-0|\-)\.0000000000 * ( \-|\-0)\.000047" $log | wc -l`
CRIT6=`$GREP "Energy at this geometry is * : * -113\.06131" $log | wc -l`
CRIT7=`$GREP "Norm of gradient * : * ( |0)\.000067" $log | wc -l`
CRIT8=`$GREP "Norm of step * : * ( |0)\.000038" $log | wc -l`
TEST[2]=`expr $CRIT1 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ $CRIT7 \+ $CRIT8` 
#        expr 1 + 3 + 1 + 1 + 1 + 1 + 1
CTRL[2]=9
ERROR[2]="FIRST ITERATION IS NOT CORRECT"

# Final summary:
CRIT1=`$GREP " C * ( |0)\.0000000000 * ( |0)\.0000000000 * 1\.234787" $log | wc -l`
CRIT2=`$GREP " O * ( |0)\.0000000000 * ( |0)\.0000000000 * ( \-|\-0)\.920516" $log | wc -l`
CRIT3=`$GREP "Energy at final geometry is * : *-113\.061313 a.u." $log | wc -l`
TEST[3]=`expr $CRIT1 \+ $CRIT2 + $CRIT3` 
#        expr 4 + 4 + 1
CTRL[3]=9
ERROR[3]="FINAL SUMMARY IS NOT CORRECT"

PASSED=1
for i in 1 2 3
do 
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} ); "
     PASSED=0
   fi
done 

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM 
  exit 1
fi
%EOF%
chmod +x cc_geoopt2.check
#######################################################################
